package com.bishe.controller;
import cn.hutool.core.util.StrUtil;
import com.bishe.pojo.Clientinfo;
import com.bishe.pojo.query.ClientQuery;
import com.bishe.pojo.query.PageResult;
import com.bishe.serivce.IClientinfoService;
import com.bishe.utils.EcoBootException;
import com.bishe.utils.Result;
import com.bishe.utils.SqlQueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 客户控制层
 * @date 2022/03/02 38:11:51
 */
@RestController
@RequestMapping("/clientinfo")
@Api(tags = "客户")
@CrossOrigin
public class ClientinfoController {

    @Autowired
    private IClientinfoService clientinfoService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ApiOperation("客户列表")
    @GetMapping("/toPage")
    public Result toPage(ClientQuery query){

        //查询所有
        SqlQueryUtils<Clientinfo> queryUtils = new SqlQueryUtils<>(Clientinfo.class,jdbcTemplate);
        // 查询名称条件不为空  迷糊查询
        if (StrUtil.isNotEmpty(query.getName())){
            queryUtils.like(Clientinfo::getName,query.getName());
        }

        // 分页
        List<Clientinfo> list = queryUtils
                .orderByDesc(Clientinfo::getCreateTime) //创建时间倒叙排序
                .limit(query.getPageNo(), query.getPageSize()) //分页
                .getList();

        //封装分页数据返回
        PageResult<Clientinfo> pageResult = new PageResult<>(query, queryUtils, list);


        return Result.ok(pageResult);
    }


    @ApiOperation("获取客户姓名列表")
    @GetMapping("/getNames")
    public Result getNames(){
        List<String> userNames = clientinfoService.getUserNames();
        return Result.ok(userNames);
    }

    @ApiOperation("根据id删除客户")
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }

        clientinfoService.deleteById(id);
        return Result.ok("删除成功");
    }


    @ApiOperation("添加或修改")
    @PostMapping("/addOrUpdate")
    public Result updateData(@RequestBody Clientinfo clientinfo){

        if (clientinfo == null){
            throw new EcoBootException("数据不能为空");
        }
        SqlQueryUtils<Clientinfo> queryUtils = new SqlQueryUtils<>(Clientinfo.class,jdbcTemplate);
        // 根据name查询单条记录
        Clientinfo one = queryUtils.eq(Clientinfo::getId, clientinfo.getId()).getOne();

        clientinfo.setCreateTime(new Date());
        // 不存在 添加
        if (one == null){
            clientinfo.setId(StrUtil.uuid());
            clientinfo.setCreateTime(new Date());
            //保存
            clientinfoService.save(clientinfo);
            return Result.ok("添加成功");
        }else{
            // 存在 修改
            clientinfoService.updateById(clientinfo);
            return Result.ok("修改成功");
        }
    }

}
